package com.woniu.intlet.web;

import com.woniu.uitl.ResponseResult;
import com.woniu.uitl.TimeUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "幂等性token接口")
public class IdempotencyController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("addRedisToken")
    public ResponseResult<String> getToken(){
        //生产随机数
        Date date = new Date();
        String nowTime = TimeUtil.getNowTime(date);
        int i = new Random().nextInt(10000);
        String redisToken = nowTime+i;
        BoundValueOperations<String, String> ops = redisTemplate.boundValueOps(redisToken);
        ops.set(redisToken);
        return new ResponseResult<String>(redisToken,"OK",200);
    }

}
